package com.mfangsoft.zhuangjialong.app.weixin.controller;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mfangsoft.zhuangjialong.app.weixin.model.QRCodeModel;
import com.mfangsoft.zhuangjialong.app.weixin.model.TouTiaoRegister;
import com.mfangsoft.zhuangjialong.app.weixin.model.WeiXinPhone;
import com.mfangsoft.zhuangjialong.app.weixin.service.IWeiXinService;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;
import com.mfangsoft.zhuangjialong.integration.region.model.RegionEntity;
import com.mfangsoft.zhuangjialong.integration.usermanage.model.BaseUserManage;

@Controller
@RequestMapping("/app")
public class WeiXinController {
	@Autowired
	IWeiXinService weiXinService;
	
	@RequestMapping(value = "/getWeiXinInfo", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Map<String, Object>> getWeiXinInfo(@RequestBody Map<String,Object> map) {
		ResponseMessage<Map<String, Object>> message = new ResponseMessage<Map<String, Object>>();
		try {
			Map<String, Object> customer=weiXinService.getWeiXinInfo(map);
			if(customer!=null){
				message.setResult(customer);
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				return message;
			}else{
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage(SysConstant.FAILURE_MSG);
				return message;
			}
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	@RequestMapping(value = "/sendSMS", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> sendSMS(@RequestBody Map<String,Object> map){
		boolean sendMS = weiXinService.sendMS(""+map.get("phone"));
		ResponseMessage<String> responseMessage = new ResponseMessage<>();
		if (sendMS) {
			responseMessage.setCode("0");
			responseMessage.setMessage("发送成功");
		} else {
			responseMessage.setCode("1");
			responseMessage.setMessage("发送失败");
		}
		return responseMessage;
	}
	@RequestMapping(value = "/registerPhone", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Map<String,Object>> registerPhone(@RequestBody Map<String,Object> map){
		ResponseMessage<Map<String,Object>> message = new ResponseMessage<>();
		try {
			return weiXinService.updateCustomer(map);
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
		
	}
	@RequestMapping(value = "/touTiaoRegister", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> touTiaoRegister(@RequestBody TouTiaoRegister touTiaoRegister){
		ResponseMessage<String> message = new ResponseMessage<>();
		try {
			return weiXinService.touTiaoRegister(touTiaoRegister);
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
		
	}
	
	@RequestMapping(value = "/makeQRCode", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> makeQRCode(@RequestBody QRCodeModel qrCodeModel) {
		ResponseMessage<String> message = new ResponseMessage<>();
		try {
			String qrCode = weiXinService.makeQRCode(qrCodeModel);
			if(qrCode!=null){
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				message.setResult(qrCode);
			}else{
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage(SysConstant.FAILURE_MSG);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return message;
	}
	
	@RequestMapping(value = "/createQRCode", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> creadeQRcOde(@RequestBody Map<String,Object> map) {
		ResponseMessage<String> message = new ResponseMessage<>();
		String qrCode;
		try {
			qrCode = weiXinService.creadeQRcOde(map);
			if(qrCode!=null){
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				message.setResult(qrCode);
			}else{
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage(SysConstant.FAILURE_MSG);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return message;
	}
	
	@RequestMapping(value = "/getprovince", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<List<RegionEntity>> getprovince(){
		ResponseMessage<List<RegionEntity>> responseMessage=new ResponseMessage<>();
		
		responseMessage.setCode(SysConstant.SUCCESS_CODE);
		responseMessage.setMessage(SysConstant.SUCCESS_MSG);
		responseMessage.setResult(weiXinService.getProvince());
		return responseMessage;
	}
	
	@RequestMapping(value = "/getcity", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<List<RegionEntity>> getshi(@RequestBody RegionEntity regionEntity){
		ResponseMessage<List<RegionEntity>> responseMessage=new ResponseMessage<>();
		
		responseMessage.setCode(SysConstant.SUCCESS_CODE);
		responseMessage.setMessage(SysConstant.SUCCESS_MSG);
		responseMessage.setResult(weiXinService.getCity(regionEntity.getCode()));
		return responseMessage;
		
	}
	
	@RequestMapping(value = "/getcountry", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<List<RegionEntity>> getcountry(@RequestBody RegionEntity regionEntity){
		ResponseMessage<List<RegionEntity>> responseMessage=new ResponseMessage<>();
		
		responseMessage.setCode(SysConstant.SUCCESS_CODE);
		responseMessage.setMessage(SysConstant.SUCCESS_MSG);
		responseMessage.setResult(weiXinService.getCounty(regionEntity.getCode()));
		return responseMessage;
		
	}
	
	/**
	 * 获取微信绑定手机号
	 */
	@RequestMapping(value = "/getPhoneNumber", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<WeiXinPhone> getPhoneNumber(@RequestBody Map<String,Object> map){
		ResponseMessage<WeiXinPhone> message = new ResponseMessage<WeiXinPhone>();
		try {
			WeiXinPhone result = weiXinService.getPhoneNumber(map);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(result);
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;	
	}
}
